package com.kedacom.gsyvideoplayer.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;

import com.kedacom.gsyvideoplayer.IVideoPlayCallBack;
import com.kedacom.gsyvideoplayer.R;
import com.kedacom.gsyvideoplayer.util.ScreenUtils;
import com.shuyu.gsyvideoplayer.utils.CommonUtil;

import java.util.Map;

/**
 * 基于MediaPlayer封装的播放器；
 * 1、没有操作工具栏
 * 2、只适合电视盒子的视频播放，通过遥控器来控制；
 * 3、具体使用参考【点播系统】：https://szgitlab.kedacom.com/AndroidTechTeam/vodsystem
 * 4、参考：com.kedacom.vodsystem.view.activity.OnDemandDetailActivity
 */
public class SuperVideoView extends RelativeLayout {

    private static final int UPDATE_PROGRESS = 1;
    //定义变量
    private static final int LEFT = 0;
    private static final int RIGHT = 1;
    private static final int CANCLE = 2;
    private static int PLAY_VIDEO_CURRENT_SPEED = 800;
    private final boolean isVerticalScreen = true;
    private final int playSpeedPosintionn = 200;
    //触摸进度dialog
    protected Dialog mProgressDialog;
    //触摸进度条的progress
    protected ProgressBar mDialogProgressBar;
    //触摸移动显示文本
    protected TextView mDialogSeekTime;
    //触摸移动显示全部时间
    protected TextView mDialogTotalTime;
    //触摸移动方向icon
    protected ImageView mDialogIcon;
    //手动改变滑动的位置
    protected int mSeekTimePosition;
    private VideoView videoView;
    private SeekBar seekbarProgress;
    private TextView tvCurrentProgress;
    private TextView tvTotalProgress;
    private ImageView btnScreen;
    private ENDownloadView loadView;
    private ImageView playPauseView;
    private LinearLayout llyController;
    private RelativeLayout rlContainer;
    private AudioManager mAudioManager;
    private int screenWidth;
    private int screenHeight;
    private ImageView fullScreenImagView;
    private Context mContext;
    private View videoLayout;
    private Activity mActivity;
    private int videoPos;
    private int state = 0;
    private String mVideoPath;
    private boolean isFullScreen = false;
    private FrameLayout flVideoPlayerContainer;
    private int playSpeedNum = 2;
    private boolean isPalySeed;
    private boolean isLeftFast;
    private boolean isRightFast;
    //程序启动时，初始化并发送消息
    private final Handler mfastHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case LEFT:
                    isPalySeed = true;
                    isLeftFast = true;
                    isRightFast = false;
                    Log.d("SuperVideoView", "LEFT");
                    fastForwardPlay(isLeftFast);
                    break;
                case RIGHT:

                    isPalySeed = true;
                    isLeftFast = false;
                    isRightFast = true;
                    fastForwardPlay(isLeftFast);
                    //moveX = moveX + playSpeedPosintionn * playSpeedNum;
                    Log.d("SuperVideoView", "RIGHT");
                    // keyMove();
                    break;
                case CANCLE:                        //停止按键
//                    firstKeyDown = true;
//                    moveX = pointX;
//                    firstKeyDown = true;
//                    tim = 2;
//                    //   onStopTrackingTouch(mProgressBar);
//                    mBottomContainer.setVisibility(GONE);
//                    // startDismissControlViewTimer();
//                    // touchSurfaceUp();
//                    // startProgressTimer();
//                    mChangePosition  = false;
//                    Log.d("TvVideoPlayer ", "CANCEL");
                    //不要和隐藏虚拟按键后，滑出虚拟按键冲突
                    break;
                default:
                    break;
            }
        }
    };
    private LinearLayout llReplayView = null;
    private IVideoPlayCallBack playVideoCallBack;
    private final Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            if (msg.what == UPDATE_PROGRESS) {

                // 获取当前时间
                int currentTime = videoView.getCurrentPosition();
                // 获取总时间
                int totalTime = videoView.getDuration();
                if (currentTime >= totalTime) {
                    videoView.stopPlayback();
                    videoView.seekTo(0);
                    seekbarProgress.setProgress(0);
//                    btnController.setImageResource(R.drawable.btn_play_style);
                    Log.d("SuperVideoView", "complete  currentTime " + currentTime + " totalTime " + totalTime);
                    playVideoCallBack.onCompletionListener(null);
                    updateTextViewFormat(tvCurrentProgress, 0);
                    mHandler.removeMessages(UPDATE_PROGRESS);
                } else {
                    seekbarProgress.setMax(totalTime);
                    seekbarProgress.setProgress(currentTime);
                    updateTextViewFormat(tvCurrentProgress, currentTime);
                    updateTextViewFormat(tvTotalProgress, totalTime);
                    mHandler.sendEmptyMessageDelayed(UPDATE_PROGRESS, 100);
                }
            }
        }
    };
    private boolean isRePlay = false;
    /**
     * 播放暂停按钮
     */
    private ImageView startPlayImge;

    public SuperVideoView(Context context) {
        super(context, null);
    }

    public SuperVideoView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SuperVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init();
        initView();
        initListener();
    }

    public void setVideoViewCallBack(IVideoPlayCallBack callBack) {
        playVideoCallBack = callBack;

    }

    public void register(Activity activity) {
        this.mActivity = activity;
    }

    /**
     * 设置媒体路径
     *
     * @param path
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void setVideoPath(String path, Map<String, String> headerMap) {

        this.mVideoPath = path;
        if (headerMap.isEmpty()) {
            videoView.setVideoURI(Uri.parse(path));
        } else {
            videoView.setVideoURI(Uri.parse(path), headerMap);
        }
        if (!loadView.isShown()) {
            loadView.setVisibility(VISIBLE);
            loadView.start();
        }
    }

    /**
     * @param path
     */
    public void setVideoPath(String path) {
        this.mVideoPath = path;
        videoView.setVideoURI(Uri.parse(path));
        if (!loadView.isShown()) {
            loadView.setVisibility(VISIBLE);
            loadView.start();
        }
    }

    public void starPlay() {
        if (!TextUtils.isEmpty(mVideoPath)) {
//            btnController.setImageResource(R.drawable.btn_pause_style);
            videoView.start();
            mHandler.sendEmptyMessage(UPDATE_PROGRESS);
            if (state == 0) {
                state = 1;
            }
        }

    }

    public void stopPlay() {
        if (videoView.isPlaying()) {
            videoView.stopPlayback();
        }
    }

    private void init() {
        screenWidth = ScreenUtils.getScreenWidth(mContext);
        screenHeight = ScreenUtils.getScreenHeight(mContext);
        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);

    }

    /**
     * UI 初始化
     */
    private void initView() {
        videoLayout = LayoutInflater.from(mContext).inflate(R.layout.video_layout, this, true);
        flVideoPlayerContainer = (FrameLayout) videoLayout.findViewById(R.id.fl_video);
        videoView = (VideoView) videoLayout.findViewById(R.id.videoView);
        seekbarProgress = (SeekBar) videoLayout.findViewById(R.id.seekbar_progress);
        playPauseView = (ImageView) videoLayout.findViewById(R.id.start);
        loadView = (ENDownloadView) videoLayout.findViewById(R.id.loading);
        fullScreenImagView = (ImageView) videoLayout.findViewById(R.id.btn_screen);
        btnScreen = (ImageView) videoLayout.findViewById(R.id.btn_screen);
        startPlayImge = (ImageView) videoLayout.findViewById(R.id.start);
        tvCurrentProgress = (TextView) videoLayout.findViewById(R.id.tv_currentProgress);
        tvTotalProgress = (TextView) videoLayout.findViewById(R.id.tv_totalProgress);
        llReplayView = (LinearLayout) videoLayout.findViewById(R.id.ll_replay);
//llReplayView
//        llyController = (LinearLayout) videoLayout.findViewById(R.id.lly_controller);
//        rlContainer = (RelativeLayout) videoLayout.findViewById(R.id.rl_container);
    }

    public View getRootPlayView() {
        return videoLayout;
    }

    /**
     * 注册事件
     */
    private void initListener() {

        btnScreen.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isVerticalScreen) {
                    mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                } else {
                    mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }
            }
        });


        /**
         * 播放进度
         */
        seekbarProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                updateTextViewFormat(tvCurrentProgress, progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // 暂停刷新
                mHandler.removeMessages(UPDATE_PROGRESS);
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (state != 0) {
                    mHandler.sendEmptyMessage(UPDATE_PROGRESS);
                }
                videoView.seekTo(seekBar.getProgress());
            }
        });


        //播放结束
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {//播放结束
                mp.setLooping(false);
                //  playVideoCallBack.onCompletionListener(mp);
                Log.d("SuperVideoView", "playend");
            }
        });
        videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                mp.setLooping(false);
                playVideoCallBack.onInfoListener(mp, what, extra);
                switch (what) {
                    case MediaPlayer.MEDIA_INFO_BUFFERING_START://开始缓冲
                        loadView.setVisibility(VISIBLE);
                        loadView.start();
                        Log.d("SuperVideoView", "MEDIA_INFO_BUFFERING_START");
                        break;
                    case MediaPlayer.MEDIA_INFO_BUFFERING_END: //缓冲结束
                        loadView.release();
                        loadView.reset();
                        loadView.setVisibility(INVISIBLE);
                        Log.d("SuperVideoView", "MEDIA_INFO_BUFFERING_END");
                        break;
                    default:
                        break;
                }
                return false;
            }
        });
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setLooping(false);
                isRePlay = false;
                playVideoCallBack.onPreparedListener(mp);
                playPauseView.setVisibility(GONE);
                llReplayView.setVisibility(View.GONE);
                loadView.release();
                loadView.reset();
                loadView.setVisibility(INVISIBLE);
                Log.d("SuperVideoView", "setOnPreparedListener");
                starPlay();

            }
        });
        videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                playVideoCallBack.onErrorListener(mp, what, extra);
                Log.d("SuperVideoView", "error");
                return false;
            }
        });
//        fullScreenImagView.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (isFullScreen) {
//                    stopFullScreen();
//                } else {
//                    startFullScreen();
//                }
//            }
//        });
    }

    /**
     * 切换尺寸
     *
     * @param width
     * @param height
     */
    public void setVideoViewScale(int width, int height) {

        ViewGroup.LayoutParams videoViewLayoutParams = videoView.getLayoutParams();
        videoViewLayoutParams.width = width;
        videoViewLayoutParams.height = height;
        videoView.setLayoutParams(videoViewLayoutParams);

        LayoutParams rlContainerLayoutParams = (LayoutParams) rlContainer.getLayoutParams();
        rlContainerLayoutParams.width = width;
        rlContainerLayoutParams.height = height;
        rlContainer.setLayoutParams(rlContainerLayoutParams);
    }

    /**
     * 格式化时间进度
     */
    private void updateTextViewFormat(TextView tv, int m) {

        String result;
        // 毫秒转成秒
        int second = m / 1000;
        int hour = second / 3600;
        int minute = second % 3600 / 60;
        int ss = second % 60;

        if (hour != 0) {
            result = String.format("%02d:%02d:%02d", hour, minute, ss);
        } else {
            result = String.format("%02d:%02d", minute, ss);
        }
        tv.setText(result);
    }

    /**
     * 设置播放的速度
     */
    public void addPlaySpeedNum() {
        if (playSpeedNum <= 5) {
            playSpeedNum++;
        }
        switch (playSpeedNum) {
            case 1:
                PLAY_VIDEO_CURRENT_SPEED = 800;
                break;
            case 2:
                PLAY_VIDEO_CURRENT_SPEED = 1000;
                break;
            case 3:
                PLAY_VIDEO_CURRENT_SPEED = 1100;
                break;
            case 4:
                PLAY_VIDEO_CURRENT_SPEED = 1200;
                break;
            case 5:
                PLAY_VIDEO_CURRENT_SPEED = 1500;
                break;
            default:
                PLAY_VIDEO_CURRENT_SPEED = 1600;
                break;
        }
        Log.d("SuperVideoView", "addPlaySpeedNum：playSpeedNum" + playSpeedNum + "seekPostion" + mSeekTimePosition);

    }

    public void onPause() {
        playPauseView.setVisibility(VISIBLE);
        videoPos = videoView.getCurrentPosition();
        videoView.stopPlayback();
        mHandler.removeMessages(UPDATE_PROGRESS);
    }

    public void onResume() {
        videoView.seekTo(videoPos);
        videoView.resume();
    }

    /**
     * 设置播放进度条样式
     *
     * @param drawable
     */
    public void setProgressBg(Drawable drawable) {
        seekbarProgress.setProgressDrawable(drawable);
    }

    /**
     * 进入全屏
     */
    public void startFullScreen() {
        if (isFullScreen)
            return;

        ViewGroup decorView = getDecorView();
        if (decorView == null)
            return;

        isFullScreen = true;

        //隐藏NavigationBar和StatusBar
        hideSysBar(decorView);

        //从当前FrameLayout中移除播放器视图
        this.removeView(flVideoPlayerContainer);
        //将播放器视图添加到DecorView中即实现了全屏
        decorView.addView(flVideoPlayerContainer);

    }

    /**
     * 退出全屏
     */
    public void stopFullScreen() {
        if (!isFullScreen)
            return;

        ViewGroup decorView = getDecorView();
        if (decorView == null)
            return;

        isFullScreen = false;

        //显示NavigationBar和StatusBar
        showSysBar(decorView);

        //把播放器视图从DecorView中移除并添加到当前FrameLayout中即退出了全屏
        decorView.removeView(flVideoPlayerContainer);
        this.addView(flVideoPlayerContainer);

//        setPlayerState(PLAYER_NORMAL);
    }

    private void showSysBar(ViewGroup decorView) {
        int uiOptions = decorView.getSystemUiVisibility();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            uiOptions &= ~View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            uiOptions &= ~View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        }
        decorView.setSystemUiVisibility(uiOptions);
        mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    public void showProgressSpeedDialog() {
        mSeekTimePosition = videoView.getCurrentPosition();
        int totalTimeDuration = videoView.getDuration();
        if (mSeekTimePosition > totalTimeDuration) {
            mSeekTimePosition = totalTimeDuration;
        }
        String seekTime = CommonUtil.stringForTime(mSeekTimePosition);
        String totalTime = CommonUtil.stringForTime(totalTimeDuration);
        showProgressDialog(1, seekTime, mSeekTimePosition, totalTime, totalTimeDuration);
    }

    public void showProgressDialog(float deltaX, String seekTime, int seekTimePosition, String totalTime, int totalTimeDuration) {
        if (mProgressDialog == null) {
            View localView = LayoutInflater.from(mActivity).inflate(R.layout.video_progress_dialog, null);
            if (localView.findViewById(R.id.duration_progressbar) instanceof ProgressBar) {
                mDialogProgressBar = ((ProgressBar) localView.findViewById(R.id.duration_progressbar));
//                if (mDialogProgressBarDrawable != null) {
//                    mDialogProgressBar.setProgressDrawable(mDialogProgressBarDrawable);
//                }
            }
            if (localView.findViewById(R.id.tv_current) instanceof TextView) {
                mDialogSeekTime = ((TextView) localView.findViewById(R.id.tv_current));
            }
            if (localView.findViewById(R.id.tv_duration) instanceof TextView) {
                mDialogTotalTime = ((TextView) localView.findViewById(R.id.tv_duration));
            }
            if (localView.findViewById(R.id.duration_image_tip) instanceof ImageView) {
                mDialogIcon = ((ImageView) localView.findViewById(R.id.duration_image_tip));
            }
            mProgressDialog = new Dialog(mActivity, R.style.video_style_dialog_progress);
            mProgressDialog.setContentView(localView);
            mProgressDialog.getWindow().addFlags(Window.FEATURE_ACTION_BAR);
            mProgressDialog.getWindow().addFlags(32);
            mProgressDialog.getWindow().addFlags(16);
            mProgressDialog.getWindow().setLayout(getWidth(), getHeight());
            WindowManager.LayoutParams localLayoutParams = mProgressDialog.getWindow().getAttributes();
            localLayoutParams.gravity = Gravity.TOP;
            localLayoutParams.width = getWidth();
            localLayoutParams.height = getHeight();
            int[] location = new int[2];
            getLocationOnScreen(location);
            localLayoutParams.x = location[0];
            localLayoutParams.y = location[1];
            mProgressDialog.getWindow().setAttributes(localLayoutParams);
        }
        if (!mProgressDialog.isShowing()) {
            mProgressDialog.show();
        }
        if (mDialogSeekTime != null) {
            mDialogSeekTime.setText(seekTime);
        }
        if (mDialogTotalTime != null) {
            mDialogTotalTime.setText(" / " + totalTime);
        }
        if (totalTimeDuration > 0)
            if (mDialogProgressBar != null) {
                mDialogProgressBar.setProgress(seekTimePosition * 100 / totalTimeDuration);
            }
        if (deltaX > 0) {
            if (mDialogIcon != null) {
                mDialogIcon.setBackgroundResource(R.drawable.video_forward_icon);
            }
        } else {
            if (mDialogIcon != null) {
                mDialogIcon.setBackgroundResource(R.drawable.video_backward_icon);
            }
        }

    }

    public void dismissProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    public boolean isPlaying() {
        if (videoView != null) {
            return videoView.isPlaying();
        }
        return false;
    }

    public int getCurrentPosition() {
        if (videoView != null) {
            return videoView.getCurrentPosition();
        }
        return 0;
    }

    public int getDuration() {
        if (videoView != null) {
            return videoView.getDuration();
        }
        return 0;
    }

    public VideoView getVideoView() {
        if (videoView != null) {
            return videoView;
        }
        return new VideoView(mActivity);
    }

    /**
     * 获取DecorView
     */
    protected ViewGroup getDecorView() {

        if (mActivity == null) return null;
        return (ViewGroup) mActivity.getWindow().getDecorView();
    }

    /**
     * 暂停播放等替换按钮图标
     */
    public void playPause() {
        if (videoView.isPlaying()) {
            startPlayImge.setVisibility(VISIBLE);
            videoView.pause();
        } else {
            startPlayImge.setVisibility(GONE);
            videoView.start();
        }
    }

    private void hideSysBar(ViewGroup decorView) {
        int uiOptions = decorView.getSystemUiVisibility();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            uiOptions |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            uiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        }
        decorView.setSystemUiVisibility(uiOptions);
        mActivity.getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * 快速播放
     */
    public void fastForwardPlay(boolean isLeftFast) {
        if (!isPalySeed) {
            onPadCenterCancel();
            return;
        }
        int delx = 0;
        if (isLeftFast) {
            delx = -1;
        } else {
            delx = 1;
        }
        Log.d("SuperVideoView", "delx" + delx);
        int playcurrentPosition = videoView.getCurrentPosition();
        Log.d("SuperVideoView", "playcurrentPosition" + playcurrentPosition);
        if (playcurrentPosition == 0) {
            if (isLeftFast) {
                playcurrentPosition = 0;
                onPadCenterCancel();
            } else {
                playcurrentPosition = 100;
            }
        }
        int playSpeed = PLAY_VIDEO_CURRENT_SPEED * playSpeedNum;
        if (playcurrentPosition < playSpeed) {

        }

        int fastAfterPostion = 0;
        if (isLeftFast) {
            fastAfterPostion = playcurrentPosition - playSpeed * 8;
            Log.d("SuperVideoView", "fastAfterPostion isLeftFast" + isLeftFast + "playSpeed*6" + playSpeed * 8);
        } else {
            fastAfterPostion = playcurrentPosition + playSpeed;
        }
        Log.d("SuperVideoView", "fastAfterPostion" + fastAfterPostion);

        int duration = getDuration();
        int progress = fastAfterPostion * 100 / (duration == 0 ? 1 : duration);

        if (isLeftFast && progress == 0) {
            onPadCenterCancel();
            isPalySeed = false;
            Log.d("SuperVideoView", "cancel");
        }
        Log.d("SuperVideoView", "progress progress" + progress);
        int totalTimeDuration = getDuration();
        if (fastAfterPostion >= totalTimeDuration) {
            fastAfterPostion = totalTimeDuration;
            onPadCenterCancel();
        }
        Log.d("SuperVideoView", "fastAfterPostion" + fastAfterPostion + "totalTimeDuration" + totalTimeDuration);
        String seekTime = CommonUtil.stringForTime(fastAfterPostion);
        String totalTime = CommonUtil.stringForTime(totalTimeDuration);
        if (progress == 100) {
            onPadCenterCancel();
        }
        if (fastAfterPostion > 0) {
            showProgressDialog(delx, seekTime, fastAfterPostion, totalTime, totalTimeDuration);
        }


        if (videoView != null) {
            Log.d("SuperVideoView", "fastAfterPostiongetGSYVideoManager");
            try {
                videoView.seekTo(fastAfterPostion);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (seekbarProgress != null) {
                seekbarProgress.setProgress(progress);
            }
            if (fastAfterPostion > 0) {
                if (isLeftFast) {
                    mfastHandler.sendEmptyMessageDelayed(LEFT, 2500);
                } else {
                    mfastHandler.sendEmptyMessageDelayed(RIGHT, 2500);
                }

            }
        }
    }

    /**
     * 结束快进快退
     */
    public void onPadCenterCancel() {
        mfastHandler.sendEmptyMessage(CANCLE);
        mfastHandler.removeMessages(RIGHT);
        mfastHandler.removeMessages(LEFT);
        playSpeedNum = 1;
        isPalySeed = false;
        dismissProgressDialog();
        mfastHandler.removeCallbacksAndMessages(null);
        resetTime();
        Log.d("SuperVideoView", "addPlaySpeedNum：playSpeedNum" + playSpeedNum + "seekPostion" + mSeekTimePosition);
    }

    //重置
    public void resetTime() {
        mfastHandler.removeMessages(CANCLE);
        mfastHandler.sendEmptyMessageDelayed(CANCLE, 2500);
    }

    /**
     * 快进
     */
    public void onPadRight() {
        if (isLeftFast) {
            mfastHandler.sendEmptyMessage(CANCLE);
            mfastHandler.removeMessages(LEFT);
            mfastHandler.sendEmptyMessageDelayed(RIGHT, 1000);
        } else {
            mfastHandler.sendEmptyMessage(RIGHT);
        }

        //mHandler.sendEmptyMessageDelayed(CANCLE, 2500);
        // resetTime();
        Log.d("SuperVideoView", "KEYCODE_DPAD_RIGHT" + "Movex:movey");
    }

    /**
     * 快退
     */
    public void onPadLef() {
        if (isRightFast) {
            mfastHandler.sendEmptyMessage(CANCLE);
            mfastHandler.removeMessages(RIGHT);
            mfastHandler.sendEmptyMessageDelayed(LEFT, 1000);
        } else {
            mfastHandler.sendEmptyMessage(LEFT);
        }

        // mHandler.sendEmptyMessageDelayed(CANCLE, 2500);
        //resetTime();
        Log.d("SuperVideoView", "KEYCODE_DPAD_LEFT" + "Movex:movey");
    }

    public void hidePlayAgain() {
        llReplayView.setVisibility(View.GONE);
        isRePlay = false;
    }

    public void showPlayAginView() {
        llReplayView.setVisibility(View.VISIBLE);
        isRePlay = true;
    }

    public boolean isRePlay() {
        return isRePlay;
    }

    public boolean isPalySeed() {
        return isPalySeed;
    }

    public void setPalySeed(boolean palySeed) {
        isPalySeed = palySeed;
    }

    public boolean isLeftFast() {
        return isLeftFast;
    }

    public void setLeftFast(boolean leftFast) {
        isLeftFast = leftFast;
    }

    public boolean isRightFast() {
        return isRightFast;
    }

    public void setRightFast(boolean rightFast) {
        isRightFast = rightFast;
    }
}
